<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <new-page-filter
      v-model:query="filterInfo.query.queryMap"
      :clear-type="clearType"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
      :slip-select-request="slipSelectRequest"
    >
    </new-page-filter>
    <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :isDynamic="false"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      >
        <template v-slot:col-orderNo="scope">
          <span>{{ scope.row.orderNo }}</span>
        </template>
      </table-container>
    </el-form>
  </page-dialog>
</template>
<script setup name="buildCard">
import { getDepotAll } from '@/api/core'
import { getCustomizeSetting } from '@/api/customize'
import { getAssetStoreOrderCustomizeList } from '@/api/asset'
import _ from 'lodash'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  visible: {
    type: Boolean,
    default: false,
  },
})

const activeName = ref('first')
// 过滤相关配置
const filterInfo = ref({
  copyQuery: {},
  query: {
    customizeUserVO: {},
    queryMap: {
      buildCardStatus: {
        queryType: 'IN',
        value: '1,2',
      },
    },
  },
  searchList: [
    {
      componentType: 'input',
      label: '资产名称/单号',
      value: 'keyword',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
const clearType = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  title: '批量建卡',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, show: true },
  ],
})
const selectRow = ref([])
const shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

const page = ref({
  page: true,
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const slipSelectRequest = ref(true)
onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
  setData()
})
function pageInfo(obj) {
  page.value = obj
}
function setData() {
  let obj = {
    typeValue: {
      label: '养护类型',
      value: 'typeValue',
      asValue: 'type',
      componentType: 'select-model',
      code: 'device_maintenance_type',
      show: false,
      hidden: true,
    },
    statusValue: {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'asset_store_order_status',
      show: false,
      hidden: true,
    },
    receiveOrderNo: {
      label: '收货单号',
      value: 'receiveNo',
      componentType: 'input',
      show: false,
      hidden: true,
    },
    depotName: {
      label: '出库仓库',
      value: 'depotName',
      asValue: 'depotId',
      componentType: 'slip-select',
      query: {
        // depotAuth: 'id',
        status: 1,
        useTypeId: '2',
      },
      api: getDepotAll,
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'assetStoreOrderList-com.slip.spd.entity.vo.device.store.AssetStoreOrderVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getAssetStoreOrderCustomizeList
      getList(1)
    }
  })
}
function search() {
  if (activeName.value == 'first') {
    getList(1)
  } else if (activeName.value == 'second') {
    getList(2)
  }
}
function clearSearch() {
  filterInfo.value.query.keyword = ''
  clearType.value = !clearType.value
  search()
}
function getList() {
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  if (event === 'tableCheck') {
    selectRow.value = data
  }
}
function handleSave() {
  emits('callback', selectRow.value)
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
</script>
<style scoped lang="scss"></style>
